/**
 * Log4jLogFactory
 *
 * @author Chris Pratt
 *
 * 10/20/2011
 */
package com.anodyzed.onyx.log.log4j;

import com.anodyzed.onyx.log.Log;
import com.anodyzed.onyx.log.LogBuilder;
import com.anodyzed.onyx.log.LogFactory;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

public class Log4jLogFactory implements LogFactory {

  @Override
  public Log getLogger () {
    StackTraceElement[] trace = Thread.currentThread().getStackTrace();
    for(int i = 0;i < trace.length;i++) {
      if(trace[i].getClassName().equals(LogBuilder.FQBN)) {
        return new Log4jLog(Logger.getLogger(trace[i + 1].getClassName()));
      }
    }
    return null;
  } //getLogger

  @Override
  public Log getLogger (Class<?> clazz) {
    return new Log4jLog(Logger.getLogger(clazz));
  } //getLogger

  @Override
  public Log getLogger (String category) {
    return new Log4jLog(Logger.getLogger(category));
  } //getLogger

  /**
   * Set the Mapped Diagnostic Context Identifier for the current thread
   *
   * @param id The Identifier String
   */
  @Override        
  public void setId (String id) {
    MDC.put("id",id);
  } //setId

  /**
   * Set the value of a Mapped Diagnostic Context key for the current thread
   *
   * @param key The MDC Key
   * @param val The MDC Value
   */
  @Override
  public void set (String key,String val) {
    MDC.put(key,val);
  } //set

} //*Log4jLogFactory
